package com.ruoyi.email.controller;

import java.util.List;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.email.domain.SysEmailMessage;
import com.ruoyi.email.service.EmailService;
import com.ruoyi.email.service.ISysEmailMessageService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 邮件消息Controller
 * 
 * @author ruoyi
 * @date 2022-04-11
 */
@RestController
@RequestMapping("/email")
public class SysEmailMessageController extends BaseController
{
    @Autowired
    private ISysEmailMessageService sysEmailMessageService;

    @Autowired
    private EmailService emailService;

    /**
     * 测试普通邮件发送
     */
    @PostMapping("/common")
    @ApiOperation(value = "普通邮件发送")
    public R common(SysEmailMessage toEmail) {
        Boolean aBoolean = emailService.commonEmail(toEmail);
        if (aBoolean) {
            return R.ok("普通邮件成功");
        } else {
            return R.fail("普通邮件发送失败");
        }
    }

    /**
     * html 类型邮件发送
     */
    @PostMapping("/html")
    @ApiOperation(value = "html 类型邮件发送")
    public R html(@RequestBody SysEmailMessage toEmail) throws MessagingException {
        Boolean aBoolean = emailService.htmlEmail(toEmail);
        if (aBoolean) {
            return R.ok("HTML 邮件成功");
        } else {
            return R.fail("HTML 邮件发送失败");
        }
    }

    /**
     * 发送附件 邮件发送
     */
    @PostMapping("/enclosure")
    @ApiOperation(value = "发送附件 类型邮件发送")
    public R enclosureEmail(SysEmailMessage toEmail, MultipartFile multipartFile) {
        Boolean aBoolean = emailService.enclosureEmail(toEmail, multipartFile);
        if (aBoolean) {
            return R.ok("附件邮件成功");
        } else {
            return R.fail("附件邮件发送失败");
        }
    }

    /**
     * 发送包含静态资源的文件
     *
     * @param toEmail 接收方
     * @param multipartFile 静态资源
     * @param resId 资源唯一Id(随意传，唯一即可）
     */
    @PostMapping("/static")
    @ApiOperation(value = "静态资源的文件 类型邮件发送")
    public R Static(SysEmailMessage toEmail, MultipartFile multipartFile, String resId) {
        Boolean aBoolean = emailService.staticEmail(toEmail, multipartFile, resId);
        if (aBoolean) {
            return R.ok("嵌入静态资源的邮件已经发送");
        } else {
            return R.fail("嵌入静态资源的邮件发送失败");
        }
    }

    /**
     * 查询邮件消息列表
     */
    @RequiresPermissions("system:email:list")
    @GetMapping("/list")
    public TableDataInfo list(SysEmailMessage sysEmailMessage)
    {
        startPage();
        List<SysEmailMessage> list = sysEmailMessageService.selectSysEmailMessageList(sysEmailMessage);
        return getDataTable(list);
    }

    /**
     * 导出邮件消息列表
     */
    @RequiresPermissions("system:message:export")
    @Log(title = "邮件消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysEmailMessage sysEmailMessage)
    {
        List<SysEmailMessage> list = sysEmailMessageService.selectSysEmailMessageList(sysEmailMessage);
        ExcelUtil<SysEmailMessage> util = new ExcelUtil<SysEmailMessage>(SysEmailMessage.class);
        util.exportExcel(response, list, "邮件消息数据");
    }

    /**
     * 获取邮件消息详细信息
     */
    @RequiresPermissions("system:message:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysEmailMessageService.selectSysEmailMessageById(id));
    }

    /**
     * 新增邮件消息
     */
    @RequiresPermissions("system:message:add")
    @Log(title = "邮件消息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysEmailMessage sysEmailMessage)
    {
        return toAjax(sysEmailMessageService.insertSysEmailMessage(sysEmailMessage));
    }

    /**
     * 修改邮件消息
     */
    @RequiresPermissions("system:message:edit")
    @Log(title = "邮件消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysEmailMessage sysEmailMessage)
    {
        return toAjax(sysEmailMessageService.updateSysEmailMessage(sysEmailMessage));
    }

    /**
     * 删除邮件消息
     */
    @RequiresPermissions("system:message:remove")
    @Log(title = "邮件消息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysEmailMessageService.deleteSysEmailMessageByIds(ids));
    }
}
